﻿namespace EasyGo.Data.QueryServices
{
    using System.Collections.Generic;
    using System.Linq;
    using EasyGo.Common.Auth;
    using EasyGo.Common.Dto;
    using EasyGo.Common.Queries;

    public class UserQueries : IUserQueries
    {
        private readonly ApplicationDbContext context;

        public UserQueries(ApplicationDbContext context)
        {
            this.context = context;
        }

        public IEnumerable<ListUserDto> GetAllUsers()
        {
            return
                this.context.Users.Select(
                    x =>
                    new ListUserDto()
                        {
                            Id = x.Id,
                            Name = x.FirstName + " " + x.LastName,
                            Email = x.Email,
                            CreateDate = x.AddDate
                        });
        }

        public UserDto FindUser(int id)
        {
            var user =
                this.context.Users.Select(
                    x =>
                    new UserDto()
                        {
                            Id = x.Id,
                            Description = x.Description,
                            Email = x.Email,
                            FirstName = x.FirstName,
                            LastName = x.LastName,
                            Role = x.Role
                        }).SingleOrDefault(x => x.Id == id);
            if (user != null)
            {
                user.RoleName = UserRole.Create(user.Role).Description;
            }

            return user;
        }
    }
}
